function getUrlParam(name) {
    var reg = new RegExp("(^|&amp;)" + name + "=([^&amp;]*)(&amp;|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}
// var id=getUrlParam("bookId");
// console.log(id);
$(document).ready(function() {
    var id = getUrlParam("bookId");
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/detail?bookId=" + id,
        data: "",
        async: false,
        success: function (data) {
            $('.left_content').empty();
            console.log(data);
            res = data;
            var bookItem = '<div class="crumb_nav">' +
                '<a href="index.html">首页</a> &gt;&gt; NAME' +
                '</div>' +
                '<div class="title"><span class="title_icon"><img src="images/bullet1.gif" alt="" title="" /></span>NAME</div>' +
                '<div class="feat_prod_box_details">' +
                '<div class="prod_img_det"><a href="details.html?bookId=' + id + '"><img src="COVER" class="imgZoomDet" border="0" /></a>' +
                '<br /><br />' +
                '<a href="COVER" rel="lightbox"><img src="images/zoom.gif" border="0" /></a>' +
                '</div>' +
                '<div class="prod_det_box_det">' +
                '<div class="box_top_det"></div>' +
                '<div class="box_center_det">' +
                '<div class="prod_title">详情</div>' +
                '<p class="details" style="height:150px;overflow: hidden;">summary</p>' +
                '<div class="price"><strong>作者:</strong> <span class="red">AUTHOR</span></div>' +
                '<div class="price"><strong>出版社:</strong> <span class="red">PUB</span></div>' +
                '<div class="price"><strong>出版时间:</strong> <span class="red">PUBDATE</span></div>' +
                '<div class="price"><strong>编号:</strong> <span class="red">ISBN</span></div>' +
                '<div class="clear"></div>' +
                '</div>' +
                '<div class="box_bottom_det"></div>' +
                '</div>' +
                '<div class="clear"></div>' +
                '</div>' +
                '<div class="clear"></div>'
            // var bookItem = '<div class="new_prod_box"><a href="details.html?bookId=ID" style="width: 150px ;height: 24px">NAME</a><div class="new_prod_bg"> <a href="details.html?bookId=ID"><img src="COVER" alt="" title="" class="thumb1" border="0" /></a></div> </div>';
            bookItem = bookItem.replace(/NAME/g, data['bookName']).replace(/COVER/g, data['bookCover']).replace('summary', data['bookSummary']).replace('AUTHOR', data['bookAuthor']).replace('PUB', data['bookPub']).replace('PUBDATE', data['bookPubdate']).replace('ISBN', data['bookIsbn']);
            // console.log(bookItem);
            $('.left_content').append(bookItem);

        }
    });
    getRateBooks();
    var username = getCookie('username');
    console.log(username);
    if (username != "null" && username != null) {
        $('#userName').text('你好'+username);
        $('#login').hide();
    } else
        $('#logout').hide()

})

function logout() {
    $.cookie('userId', null);
    $.cookie('username', null);
    location.reload();
}

function getRateBooks(){
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/randBooks",
        data: "",
        async: false,
        success:function (data) {
            $('.rand_books').empty();
            // totalPage = (data.bookCount + 6 - 1) / 6;
            res = data;
            var count = 0;
            for(each in data)
            {
                if (count === 3) {
                    break;
                }
                var bookItem = '<div class="new_prod_box">\n' +
                    '                        <a href="details.html?bookId=BOOKID">BOOKNAME</a>\n' +
                    '                        <div class="new_prod_bg">\n' +
                    '                            <span class="new_icon"><img src="images/promo_icon.gif" alt="" title=""/></span>\n' +
                    '                            <a href="details.html?bookId=BOOKID"><img src="COVER" alt="" title="" class="thumb"\n' +
                    '                                                        border="0"/></a>\n' +
                    '                        </div>\n' +
                    '                    </div>';
                bookItem = bookItem.replace('BOOKNAME', data[each]['bookName']).replace(/BOOKID/g, data[each]['bookId']).replace('COVER', data[each]['bookCover']);
                $('.rand_books').append(bookItem);
                count++;
            }
        }
    });
}


function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
